from functools import wraps
from django.db import transaction
from common.core.response import success, error
from common.core.exceptions import BusinessError, ValidationError
from common.core.constants import ErrorCode

def atomic_transaction(func):
    """
    数据库事务装饰器
    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            with transaction.atomic():
                return func(*args, **kwargs)
        except BusinessError as e:
            return error(
                message=str(e.detail),
                code=e.code
            )
        except ValidationError as e:
            return error(
                message=str(e.detail),
                code=e.code
            )
        except Exception as e:
            return error(
                message=str(e),
                code=ErrorCode.SERVER_ERROR
            )
    return wrapper 